<template>
  <el-dialog title="分配角色" :visible.sync="dialogShow">
    <el-checkbox-group v-model="checkList">
      <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <div style="text-align:center;margin-top:20px;">
      <el-button type="primary" size="small" @click="save">确定</el-button>
      <el-button size="small" @click="dialogShow = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { roleListAPI } from '@/api/role'
import { profile, assignRole } from '@/api/employees'
export default {
  data() {
    return {
      dialogShow: false,
      checkList: [],
      roleList: [],
      userId: ''
    }
  },
  methods: {
    async show(userId) {
      // 1. 查出所有的角色列表
      const res = await roleListAPI({
        page: 1,
        pagesize: 99999999
      })
      this.userId = userId
      this.roleList = res.data.rows
      // 2. 把当前用户所对应的角色设置默认选中-查出当前用户信息
      const res2 = await profile(userId)
      this.checkList = res2.data.roleIds
      // 3. 显示出对话框
      this.dialogShow = true
    },

    async save() {
      // 1. 发请求，把选中的数据给接口
      await assignRole({
        id: this.userId, // 代表给哪个员工分配角色,
        roleIds: this.checkList // 分配哪些角色，要传入的数组，数组里每个元素是角色id
      })
      this.$message.success('分配角色成功')
      // 2. 隐藏对话框
      this.dialogShow = false
    }
  }
}
</script>

<style></style>
